﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using MillerApp.CommonLib.LogProvider;

namespace MillerApp.CommonLib.Utility
{
    public class SocketHelper
    {
        ILogProvider log;
        public SocketHelper(ILogProvider logProvider)
        {
            this.log = logProvider;
        }

        /// <summary>
        /// 判断能否开启Socket通道
        /// </summary>
        /// <param name="hostName">目标主机地址</param>
        /// <param name="port">目标主机端口</param>
        /// <returns></returns>
        public bool CanOpenSocket(string hostName, int port)
        {
            bool flag = false;
            TcpClient client = null;
            try
            {
                client = new TcpClient();
                client.Connect(hostName, port);
                flag = true;
            }
            catch (Exception exception)
            {
                log.WriteLog("" + exception.Message);
            }
            finally
            {
                if (client.Connected)
                {
                    client.Close();
                }
            }
            return flag;
        }

 
    }
}
